﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace TimKen.Web.User
{
    public partial class Inbox : TimKen.App.WebPage
    {

        TimKen.BussLogic.InboxManager inboxM = new TimKen.BussLogic.InboxManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CurrentUserName == null || CurrentUserName == "")
                {
                    Alert("您必须登录！", "../Index.aspx");
                }          
                BindData();
                BindData2();
            }
        }

        protected void BindData()
        {
            int isRead = -1;
            if (chkIsRead.Checked)
                isRead = 0;
            DataSet ds = inboxM.GetList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, CurrentUserName, "", isRead);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0]["total"]);

            this.grdInbox.DataSource = ds.Tables[0].DefaultView;
            this.grdInbox.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void grdInbox_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DeleteMessage")
            {
                int msgID = int.Parse(e.CommandArgument.ToString());
                TimKen.DataEntity.Inbox inbox = inboxM.GetModel(msgID);
                if (inbox.FromDelete)
                {
                    inboxM.Delete(msgID);
                }
                else
                {
                    inbox.ToDelete = true;
                    inboxM.Update(inbox);
                }
                BindData();
            }
        }

        protected void BindData2()
        {
            DataSet ds2 = inboxM.GetList(AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex, "", CurrentUserName, -1);
            AspNetPager2.RecordCount = Convert.ToInt32(ds2.Tables[1].Rows[0]["total"]);

            this.grdInbox2.DataSource = ds2.Tables[0].DefaultView;
            this.grdInbox2.DataBind();
        }

        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            BindData2();
        }

        protected void grdInbox2_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DeleteMessage")
            {
                int msgID = int.Parse(e.CommandArgument.ToString());
                TimKen.DataEntity.Inbox inbox = inboxM.GetModel(msgID);
                if (inbox.ToDelete)
                {
                    inboxM.Delete(msgID);
                }
                else
                {
                    inbox.FromDelete = true;
                    inboxM.Update(inbox);
                }
                BindData2();
            }
        }
                  
        public string GetContent(object content)
        {
            if (content == null)
                return "";

            if (string.IsNullOrEmpty(content.ToString()))
                return "";

            if (content.ToString().Length > 200)
                return content.ToString().Trim().Substring(0, 80) + "....";

            return content.ToString();

        }

        public string GetMessageStatusPic(object IsRead)
        {
            if (bool.Parse(IsRead.ToString()))
            {
                return "<img src='../Images/Read.gif' alt='" + GetLangString("MessageIsRead") + "' />";
            }
            else
            {
                return "<img src='../Images/UnRead.gif' alt='" + GetLangString("MessageUnRead") + "' />";
            }
        }

        protected void chkIsRead_CheckedChanged(object sender, EventArgs e)
        {
            BindData();
        }
    }
}
